package com.ruoyi.vip.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.cyl.manager.vip.domain.entity.VipOrder;
import com.cyl.manager.vip.service.IVipOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * VIP订单管理Controller
 *
 * @author system
 */
@RestController
@RequestMapping("/vip/order")
public class VipOrderController extends BaseController {
    @Autowired
    private IVipOrderService vipOrderService;

    /**
     * 查询VIP订单列表
     */
    @PreAuthorize("@ss.hasPermi('vip:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(VipOrder vipOrder) {
        startPage();
        List<VipOrder> list = vipOrderService.selectVipOrderList(vipOrder);
        return getDataTable(list);
    }


    /**
     * 获取VIP订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('vip:order:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(vipOrderService.selectVipOrderById(id));
    }

    /**
     * 新增VIP订单
     */
    @PreAuthorize("@ss.hasPermi('vip:order:add')")
    @Log(title = "VIP订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VipOrder vipOrder) {
        return toAjax(vipOrderService.insertVipOrder(vipOrder));
    }

    /**
     * 修改VIP订单
     */
    @PreAuthorize("@ss.hasPermi('vip:order:edit')")
    @Log(title = "VIP订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VipOrder vipOrder) {
        return toAjax(vipOrderService.updateVipOrder(vipOrder));
    }

    /**
     * 删除VIP订单
     */
    @PreAuthorize("@ss.hasPermi('vip:order:remove')")
    @Log(title = "VIP订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(vipOrderService.deleteVipOrderByIds(ids));
    }

    /**
     * 根据订单号查询VIP订单
     */
    @PreAuthorize("@ss.hasPermi('vip:order:query')")
    @GetMapping("/sn/{orderSn}")
    public AjaxResult getByOrderSn(@PathVariable("orderSn") String orderSn) {
        VipOrder vipOrder = vipOrderService.selectVipOrderByOrderSn(orderSn);
        return AjaxResult.success(vipOrder);
    }

    /**
     * 根据用户ID查询VIP订单列表
     */
    @PreAuthorize("@ss.hasPermi('vip:order:query')")
    @GetMapping("/user/{userId}")
    public AjaxResult getByUserId(@PathVariable("userId") Long userId) {
        List<VipOrder> list = vipOrderService.selectVipOrderListByUserId(userId);
        return AjaxResult.success(list);
    }


    /**
     * 取消订单（管理员操作）
     */
    @PreAuthorize("@ss.hasPermi('vip:order:edit')")
    @Log(title = "取消VIP订单", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel/{orderSn}")
    public AjaxResult cancelOrder(@PathVariable("orderSn") String orderSn) {
        boolean result = vipOrderService.cancelOrder(orderSn);
        return result ? AjaxResult.success("订单取消成功") : AjaxResult.error("订单取消失败");
    }

    /**
     * 获取订单统计信息
     */
    @PreAuthorize("@ss.hasPermi('vip:order:list')")
    @GetMapping("/statistics")
    public AjaxResult getOrderStatistics() {
        // 查询各状态订单数量
        VipOrder pendingQuery = new VipOrder();
        pendingQuery.setStatus(0);
        int pendingCount = vipOrderService.selectVipOrderList(pendingQuery).size();

        VipOrder paidQuery = new VipOrder();
        paidQuery.setStatus(1);
        int paidCount = vipOrderService.selectVipOrderList(paidQuery).size();

        VipOrder cancelledQuery = new VipOrder();
        cancelledQuery.setStatus(2);
        int cancelledCount = vipOrderService.selectVipOrderList(cancelledQuery).size();

        VipOrder refundedQuery = new VipOrder();
        refundedQuery.setStatus(3);
        int refundedCount = vipOrderService.selectVipOrderList(refundedQuery).size();

        OrderStatistics statistics = new OrderStatistics();
        statistics.setPendingCount(pendingCount);
        statistics.setPaidCount(paidCount);
        statistics.setCancelledCount(cancelledCount);
        statistics.setRefundedCount(refundedCount);
        statistics.setTotalCount(pendingCount + paidCount + cancelledCount + refundedCount);

        return AjaxResult.success(statistics);
    }

    /**
     * 订单统计信息对象
     */
    public static class OrderStatistics {
        private int pendingCount;    // 待支付订单数
        private int paidCount;       // 已支付订单数
        private int cancelledCount;  // 已取消订单数
        private int refundedCount;   // 已退款订单数
        private int totalCount;      // 总订单数

        public int getPendingCount() {
            return pendingCount;
        }

        public void setPendingCount(int pendingCount) {
            this.pendingCount = pendingCount;
        }

        public int getPaidCount() {
            return paidCount;
        }

        public void setPaidCount(int paidCount) {
            this.paidCount = paidCount;
        }

        public int getCancelledCount() {
            return cancelledCount;
        }

        public void setCancelledCount(int cancelledCount) {
            this.cancelledCount = cancelledCount;
        }

        public int getRefundedCount() {
            return refundedCount;
        }

        public void setRefundedCount(int refundedCount) {
            this.refundedCount = refundedCount;
        }

        public int getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }
    }
}
